﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL;
using System.Collections.Generic;
using Exportation;

namespace WebSiteAlex
{
    public partial class ControlExportation : System.Web.UI.UserControl
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            this.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //String s = this.ddl_ExportationTypes.SelectedValue;
            //this.DataBind();
            //s = this.ddl_ExportationTypes.SelectedValue;
        }

        public override void DataBind()
        {
            //using (DataAccessDataContext dc = new DataAccessDataContext())
            //{
                this.ddl_ExportationTypes.DataSource = ExportationType.GetAll();
                //    var query = from t in dc.ExportationTypes
                //                select t;
                //    IEnumerable<ExportationType> etypes = query.AsEnumerable<ExportationType>();
                //    List<ExportationType> l = etypes.ToList<ExportationType>();
                //    ExportationType[] arr = new ExportationType[] { };

                //this.ddl_ExportationTypes.DataSource = etypes;
                //var query = from er in dc.ExpenseReports
                //            where !(from e in dc.ExpenseReports
                //                from t in dc.ExpenseExportations
                //                where t.ExpenseReport.Equals(e) && t.Type.Equals("ListingExcelll")
                //                select e).Contains<ExpenseReport>(er)
                //            select er;

                //this.LinqDataSource1.Where =
                base.DataBind();
            //}
        }

        protected void bt_Export_Click(object sender, EventArgs ea)
        {
            foreach (GridViewRow row in this.gv_reports.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("ReportsSelector");
                if (cb != null && cb.Checked)
                {
                    Guid g = (Guid)this.gv_reports.DataKeys[row.RowIndex].Value;
                    ExpenseReport er = ExpenseReport.Get(g);
                    if(er != null)
                        er.Export(this.ddl_ExportationTypes.SelectedValue);
                    //using (DataAccessDataContext da = new DataAccessDataContext())
                    //{
                    //    ExpenseReport report = null;
                    //    var query = from e in da.ExpenseReports
                    //                where e.Id.Equals(g)
                    //                select e;
                    //    if (query != null)
                    //    {
                    //        report = query.Single<ExpenseReport>();
                    //        //ExcelExportation ee = new ExcelExportation();
                    //        IExportation ee = ExportationFactory.GetExportationType("Exportation.ExportationTypes.ExcelExportation, DAL");
                    //        String path = ee.Export(report);
                    //        ExpenseExportation expExport = new ExpenseExportation();
                    //        expExport.Path = path;
                    //        expExport.ReportId = report.Id;
                    //        expExport.Type = "ListingExcel";
                    //        da.ExpenseExportations.InsertOnSubmit(expExport);
                    //        da.SubmitChanges();
                    //    }
                    //}
                }
            }
        }

        protected void ddl_ExportationTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected override void OnPreRender(EventArgs e)
        {
            //base.OnPreRender(e);
            //String s = this.ddl_ExportationTypes.SelectedValue;
            //this.DataBind();
        }
    }
}